﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebAPI_Demo.Models;
using WebAPI_Demo.Models.Database;

namespace WebAPI_Demo.Controllers
{

    /// <summary>
    /// 商品分类控制器
    /// </summary>

    //控制器的路由：api/控制器名字(Category)/方法名
    [Route("api/[controller]/[action]")]
    //API控制器的标识
    [ApiController]
    public class CategoryController : ControllerBase
    {
        //定义数据库上下文
        private readonly Store_2022Context _db;

        /// <summary>
        /// 构造方法 构造注入
        /// 把数据库上下文初始化
        /// </summary>
        public CategoryController(Store_2022Context db)
        {
            _db = db;
        }


        /// <summary>
        /// 新增分类
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddCategory(Category category)
        {
            _db.Categories.Add(category);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "新增分类1成功";
            }
            else
            {
                return "新增分类1失败";
            }

        }


        /// <summary>
        /// 新增分类2
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddCategory2(AddCategory2 category)
        {
            _db.Categories.Add(new Category
            {
                CateName = category.CateName
            });
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "新增分类2成功";
            }
            else
            {
                return "新增分类2失败";
            }

        }


        /// <summary>
        /// 新增分类3
        /// </summary>
        /// <param name="catename"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddCategory3(string catename)
        {
            _db.Categories.Add(new Category
            {
                CateName = catename
            });
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "新增分类3成功";
            }
            else
            {
                return "新增分类3失败";
            }

        }


        /// <summary>
        /// 更新分类信息
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        [HttpPost]
        public string UpdateCategory(UpdateCategory category)
        {
            var cate = _db.Categories.FirstOrDefault(x => x.Id == category.CateId);
            if (cate == null)
            {
                return "分类不存在";
            }
            cate.Id = category.CateId;
            cate.CateName = category.CateName;
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "更新成功";
            }
            else
            {
                return "更新失败";
            }

        }


        /// <summary>
        /// 删除分类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public string DeleteCategory(int id)
        {
            var cate = _db.Categories.FirstOrDefault(x => x.Id == id);
            if (cate == null)
            {
                return "查不到该分类";
            }
            _db.Remove(cate);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "删除成功";
            }
            else
            {
                return "删除失败";
            }
        }


        /// <summary>
        /// 查询分类
        /// </summary>
        /// <param name="id"></param>
        /// <param name="KeyWord"></param>
        /// <returns></returns>
        [HttpGet]
        public List<Category> SelectCategory(int id, string KeyWord)
        {
            var categoryList = _db.Categories.AsQueryable();
            if (id > 0)
            {
                categoryList = categoryList.Where(x => x.Id == id);
            }
            else
            {
                categoryList = categoryList.Where(x => x.CateName.Contains(KeyWord));
            }
            return categoryList.ToList();
        }
    }
}
